//
//  PrizeTableVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/9.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "PrizeTableVC.h"
#import "AppHeader.h"
#import "MyInfoTableCell.h"
#import "PrizeTableCell.h"
#import "PrizeFooderView.h"

#import "ExchangeVC.h"
#import "ApplicantViewController.h"

#import "PrizeModel.h"


@interface PrizeTableVC ()<ApplicationVCSubmitDelegate> {
    BOOL selectIndexs[500];
    NSInteger currentPage;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<PrizeModel *> *dataArray;

@property (strong, nonatomic) PrizeFooderView *footerView;

@end

@implementation PrizeTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray array];
    if (_indexRow ==2) {
        self.footerView =[PrizeFooderView loadNib:@"PrizeFooderView" frame:CGRectZero];
        _footerView.backgroundColor =[UIColor whiteColor];
        [self.view addSubview:_footerView];
        [_footerView didButtonClickBlock:^(UIButton *button) {
            NSMutableArray<PrizeModel *> *indexs =[NSMutableArray array];
            for (NSInteger i=0; i <self.dataArray.count; i++) {
                if (selectIndexs[i]) {
                    [indexs addObject:self.dataArray[i]];
                }
            }
            NSLog(@"选择的索引=%@", indexs);
            if (indexs.count>0) {
                if (button ==_footerView.applyButton) {
                    ApplicantViewController *VC =[[ApplicantViewController alloc]init];
                    VC.delegate = self;
                    VC.title =@"发货申请";
                    VC.dataArray =indexs;
                    [self.navigationController pushViewController:VC animated:YES];
                } else if (button ==_footerView.exchangeButton) {
                    ExchangeVC *VC =[[ExchangeVC alloc]init];
                    VC.title =@"兑换金豆";
                    VC.dataArray =indexs;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
        }];
    }
    currentPage = 1;
    [self.dataArray removeAllObjects];
    [self _requestNetworkShowingHud:YES];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        [self.dataArray removeAllObjects];
        [self.tableView.mj_footer setState:MJRefreshStateIdle];
        [self _requestNetworkShowingHud:NO];
    }];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        currentPage++;
        [self _requestNetworkShowingHud:NO];
    }];
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    if (self.footerView) {
        self.footerView.frame =CGRectMake(0, CGRectGetHeight(self.view.frame)-70, KScreenWidth, 70);
    }
}


#pragma mark - Request Network
- (void)_requestNetworkShowingHud:(BOOL)show {
 
    
    NSMutableDictionary *dict = @{@"current":@(currentPage)}.mutableCopy;
    if (self.indexRow == 0) {

    } else {
        [dict setObject:@(self.indexRow) forKey:@"status"];
    }
//    switch (self.indexRow) {
//        case 0:
//            [dict setObject:[NSNull null] forKey:@"status"];
//            break;
//        case 1:
//            dict setObject:<#(nonnull id)#> forKey:<#(nonnull id<NSCopying>)#>
//            break;
//        case 2:
//            break;
//        case 3:
//            break;
//        default:
//            break;
//    }
//
    
    /// 请求数据再刷新
    [HttpUrl NetErrorGET:@"myPrize/selectMyPrizeRecords" dict:dict hud:self.view isShow:show WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            NSArray *tempArr = data[@"data"];
            if (tempArr.count == 0) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                return ;
            }
            [tempArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary *dic = (NSDictionary *)obj;
                PrizeModel *prizeModel = [PrizeModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:prizeModel];
            }];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            for (NSInteger i=0; i <self.dataArray.count; i++) {
                selectIndexs[i] =NO;
            }
            [self.tableView reloadData];
        } else {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            ShowAlertTitle(data[@"msg"]);
        }
    }WithFailBlock:^(id data) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        ShowAlertTitle(@"网络连接错误");
    }];
}

#pragma mark - Public Method
/// 刷新视图
- (void)reloadTableView {
   
    currentPage = 1;
    [self.tableView.mj_footer setState:MJRefreshStateIdle];
    [self.dataArray removeAllObjects];
    [self _requestNetworkShowingHud:YES];
    
}

#pragma mark - ApplicationVCSubmitDelegate
- (void)prizeSubmitResult:(BOOL)isSuccess {
    [self reloadTableView];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 150;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (self.indexRow ==2) {
        return 70;
    }
    return 0.1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0.1)];
    headerView.backgroundColor =tableView.backgroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0.1)];
    footerView.backgroundColor =tableView.backgroundColor;
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PrizeTableCell *cell =[tableView dequeueReusableCellWithIdentifier:@"PrizeTableCell"];
    if (!cell) {
        cell =[PrizeTableCell findCellWithIdentifier:@"PrizeTableCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    [cell didButtonClickBlock:^(UIButton *button) {
        cell.selectedCell =!cell.selectedCell;
        selectIndexs[indexPath.row] =cell.selectedCell;
    }];
    PrizeModel *model = self.dataArray[indexPath.row];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld", model.prizeNumber];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@", model.goods.goodsTitle];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@", model.gmtDatetime];
    [cell.prizeImage sd_setImageWithURL:[NSURL URLWithString:model.goods.coverUrl] placeholderImage:[UIImage imageNamed:@"quanxi-dianji"]];
    
    
    if (self.indexRow ==2) {
        cell.selectedCell =selectIndexs[indexPath.row];
    } else {
        NSString *tempStr = @"";
        // 1已中奖 2待领奖 3已兑换 4待发货 5已发货
        switch (model.status) {
            case 1:
                tempStr = @"已中奖";
                break;
            case 2:
                tempStr = @"待领奖";
                break;
            case 3:
                tempStr = @"已兑换";
                break;
            case 4:
                tempStr = @"待发货";
                break;
            case 5:
                tempStr = @"已发货";
                break;
            default:
                break;
        }
        cell.typeText = tempStr;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击cell");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
